package com.ruoyi.orderadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.orderadmin.mapper.OaOrderMapper;
import com.ruoyi.orderadmin.service.IOaOrderService;
import com.ruoyi.orderadmin.domain.OaOrder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.useradmin.service.IUaUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OaOrderServiceImpl implements IOaOrderService {


    @Resource
    private OaOrderMapper oaOrderMapper;
    
    
    @Resource
    private IUaUserService uaUserService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param oaOrder 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<OaOrder> getOaOrderPage(PageDomain pageDomain, OaOrder oaOrder) {
        if (Objects.isNull(oaOrder)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<OaOrder> queryWrapper = new LambdaQueryWrapper<>(new OaOrder());
            //if(!Objects.isNull(oaOrder.getName())){
            //	queryWrapper.like(OaOrder::getName,oaOrder.getName());
            //}
            

            if (!Objects.isNull(oaOrder.getId())) {
                queryWrapper.eq(OaOrder::getId, oaOrder.getId());
            }

            if (!Objects.isNull(oaOrder.getUaUserId())) {
                queryWrapper.eq(OaOrder::getUaUserId, oaOrder.getUaUserId());
            }

            if (!Objects.isNull(oaOrder.getAllFee())) {
                queryWrapper.like(OaOrder::getAllFee, oaOrder.getAllFee());
            }

            if (!Objects.isNull(oaOrder.getStatus())) {
                queryWrapper.like(OaOrder::getStatus, oaOrder.getStatus());
            }

            if (!Objects.isNull(oaOrder.getDelFlag())) {
                queryWrapper.like(OaOrder::getDelFlag, oaOrder.getDelFlag());
            }

            if (!Objects.isNull(oaOrder.getCreateBy())) {
                queryWrapper.like(OaOrder::getCreateBy, oaOrder.getCreateBy());
            }

            if (!Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeBeginTime"), oaOrder.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(oaOrder.getUpdateBy())) {
                queryWrapper.like(OaOrder::getUpdateBy, oaOrder.getUpdateBy());
            }

            if (!Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeBeginTime"), oaOrder.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(oaOrder.getRemark())) {
                queryWrapper.like(OaOrder::getRemark, oaOrder.getRemark());
            }
        Page<OaOrder> oaOrderList= oaOrderMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(OaOrder record:oaOrderList.getRecords()){
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
        }

            return oaOrderList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public OaOrder getOaOrderById(Long id) {
        if (Objects.isNull(id)) {
            return new OaOrder();
        }
        try {
            OaOrder oaOrder = oaOrderMapper.selectById(id);
            oaOrder.setUaUser(uaUserService.getUaUserById(oaOrder.getUaUserId()));
            if (Objects.isNull(oaOrder)){
                oaOrder = new OaOrder();
            }
            return oaOrder;

        } catch (Exception e) {
            e.printStackTrace();
            return new OaOrder();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param oaOrder 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByOaOrder(OaOrder oaOrder) {
        if (Objects.isNull(oaOrder)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<OaOrder> queryWrapper = new LambdaQueryWrapper<>(new OaOrder());
            

            if (!Objects.isNull(oaOrder.getId())) {
                queryWrapper.eq(OaOrder::getId, oaOrder.getId());
            }

            if (!Objects.isNull(oaOrder.getUaUserId())) {
                queryWrapper.eq(OaOrder::getUaUserId, oaOrder.getUaUserId());
            }

            if (!Objects.isNull(oaOrder.getAllFee())) {
                queryWrapper.like(OaOrder::getAllFee, oaOrder.getAllFee());
            }

            if (!Objects.isNull(oaOrder.getStatus())) {
                queryWrapper.like(OaOrder::getStatus, oaOrder.getStatus());
            }

            if (!Objects.isNull(oaOrder.getDelFlag())) {
                queryWrapper.like(OaOrder::getDelFlag, oaOrder.getDelFlag());
            }

            if (!Objects.isNull(oaOrder.getCreateBy())) {
                queryWrapper.like(OaOrder::getCreateBy, oaOrder.getCreateBy());
            }

            if (!Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(oaOrder.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(oaOrder.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(OaOrder::getCreateTime, oaOrder.getParams().get("CreateTimeBeginTime"), oaOrder.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(oaOrder.getUpdateBy())) {
                queryWrapper.like(OaOrder::getUpdateBy, oaOrder.getUpdateBy());
            }

            if (!Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(oaOrder.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(oaOrder.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(OaOrder::getUpdateTime, oaOrder.getParams().get("UpdateTimeBeginTime"), oaOrder.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(oaOrder.getRemark())) {
                queryWrapper.like(OaOrder::getRemark, oaOrder.getRemark());
            }

            List<OaOrder> oaOrders = oaOrderMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (OaOrder oaOrderNo:oaOrders){
                idList.add(oaOrderNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<OaOrder> queryWrapper = new LambdaQueryWrapper<>(new OaOrder());
            queryWrapper.in(OaOrder::getUaUserId, uaUserIdList);
            List<OaOrder> oaOrders = oaOrderMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (OaOrder oaOrder:oaOrders){
                idList.add(oaOrder.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param oaOrder 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertOaOrder(OaOrder oaOrder) {
        if (Objects.isNull(oaOrder)) {
            return -1;
        }
        try {
        
            //oaOrder.setSpell(PinYin.getPinYin(oaOrder.getName()));
            return oaOrderMapper.insert(oaOrder);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param oaOrder 实体类
     * @return 结果
     */
    @Override
    public int updateOaOrderById(OaOrder oaOrder) {
        if (Objects.isNull(oaOrder)) {
            return -1;
        }
        try {
            //oaOrder.setSpell(PinYin.getPinYin(oaOrder.getName()));
            return oaOrderMapper.updateById(oaOrder);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteOaOrderByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return oaOrderMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<OaOrder> getOaOrderList() {
        try {
            LambdaQueryWrapper<OaOrder> queryWrapper = new LambdaQueryWrapper<>(new OaOrder());
            queryWrapper.isNotNull(OaOrder::getId);
            return oaOrderMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
